#!/usr/bin/python
#  Copyright (c) 2012 njjs Inc. All rights reserved.
#  Author : bianxf.
import sys,os,string
from datetime import date
today=date.today()
    
filename=sys.argv[1]
headcontext = filename
headcontext = headcontext+'_'

headcontext=string.upper(headcontext)
print(headcontext)
pos=headcontext.find("QDA");
if pos != -1:
    headcontext=headcontext[pos:]
headcontext=string.replace(headcontext,':','_')
headcontext=string.replace(headcontext,'\\','_')
headcontext=string.replace(headcontext,'/','_')
headcontext=string.replace(headcontext,'.','__')
headcontext='_'+headcontext
print(headcontext)


f1=open(filename,'r')

f2=open(".mytmpfile",'w+')

for line in f1:
	f2.writelines(line)

f1.close()
f2.close()

f3=open(".mytmpfile",'r')


f = open(filename,'w+')

f.writelines("/* \n* Copyright (c) 2012 XXX Inc. All rights reserved.\n")
f.writelines("* Author : XXX.\n* Brief  : \n* Histroy: create "+str(today.year)+"-"+str(today.month)+"-"+str(today.day) + "\n*/\n\n")

f.writelines("#ifndef "+headcontext+"//NOLINT"+"\n")
f.writelines("#define "+headcontext+"//NOLINT"+"\n\n")


def hasStr(dstr,fstr):
	if dstr.find(fstr) != -1:
		return 1
	else:
		return 0

flag=0
for line in f3:
	if (hasStr(line,"#ifndef") or hasStr(line,"#define")) and (hasStr(line,"_h_") or hasStr(line,"_H_")):
		print(" * *find "+line)
		flag=1
	elif (hasStr(line,"* Copyright")) or (hasStr(line,"* Author")) or hasStr(line,"* Brief")  or hasStr(line,"* Histroy") :
	    continue
	elif hasStr(line,"#pragma") and hasStr(line,"once"):
		print(" * find "+line)
	else:	
	    f.writelines(line)
	    print("   add "+line)

if flag == 0:
    f.write("\n#endif //"+headcontext+"//NOLINT\n")
else :
	f.write("//"+headcontext+"//NOLINT")

f.close()

f3.close();
os.remove(".mytmpfile")





